# coding=utf-8
import logging
import configparser
import os
import logging
import codecs

from src.public import Public


class GetConfig(object):

    projectdir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

    def returnConfig(self):
        config = configparser.ConfigParser()
        config_path = self.projectdir + r"\config\config.ini"

        with open(file=config_path, mode="rb") as f: # 2018-6-10 BEGIN 判断文件的编码类型 如果是UTF-8 BOM格式，则将其转化为无BOM格式
            s = f.read()
        if s.startswith(codecs.BOM_UTF8): # 带BOM的文件是以 b'\xef\xbb\xbf' 开头
            s = s[len(codecs.BOM_UTF8):]  # 截取 b'\xef\xbb\xbf' 到文件结尾
            with open(file=config_path, mode="wb") as f: # 保存为无BOM格式
                f.write(s)
        coding = Public().getFileCoding(filepath=config_path).get("encoding")
        config.read(filenames=config_path, encoding=coding) # 2018-6-10 END 判断文件的编码类型 如果是UTF-8 BOM格式，则将其转化为无BOM格式
        return config

    @property
    def projectDir(self):
        # return self.returnConfig().get(section="PROJECT", option="projectDir")
        # 不在需要配置项目路径，项目迁移更加灵活。
        return self.projectdir

    @property
    def browserType(self):
        return self.returnConfig().get(section="PROJECT", option="browserType")

    @property
    def timeOut(self):
        return self.returnConfig().getint(section="TEST", option="timeOut")

    @property
    def loggingLevel(self):
        level = self.returnConfig().get(section="LOG", option="loggingLevel")
        if str.upper(level) == "DEBUG":
            return logging.DEBUG
        elif str.upper(level) == "INFO":
            return logging.INFO
        elif str.upper(level) == "WARN" or str.upper(level) == "WARNING":
            return logging.WARNING
        elif str.upper(level) == "CRITICAL" or str.upper(level) == "FATAL":
            return logging.CRITICAL
        elif str.upper(level) == "FATAL":
            return logging.FATAL
        elif str.upper(level) == "ERROR":
            return logging.ERROR
        else:
            raise TypeError("配置文件中定义的日志级别loggingLevel为%s,请填写以下几种级别“INFO、DEBUG、WARNING、ERROR、CRITICAL”" % (level))


    # @property
    # def loggingFormat(self):
    #     return self.returnConfig().get(section="LOG", option="loggingFormat")

    @property
    def SMTPLoginName(self):
        return self.returnConfig().get(section="EMAIL", option="SMTPLoginName")

    @property
    def SMTPLoginPasswd(self):
        return self.returnConfig().get(section="EMAIL", option="SMTPLoginPasswd")

    @property
    def senderAddress(self):
        return self.returnConfig().get(section="EMAIL", option="senderAddress")

    @property
    def receiversAddress(self):
        address = self.returnConfig().items(section="receiversAddress") # [(,), (,)...]
        return list(dict(address).values())

    @property
    def SMTPServerAddress(self):
        return self.returnConfig().get(section="EMAIL", option="SMTPServerAddress")

    @property
    def SMTPPort(self):
        return self.returnConfig().getint(section="EMAIL", option="SMTPPort")

    @property
    def emailTitle(self):
        return self.returnConfig().get(section="EMAIL", option="emailTitle")

    @property
    def emailText(self):
        return self.returnConfig().get(section="EMAIL", option="emailText")


class Config(object):
    # 项目路径
    projectDir = GetConfig().projectDir
    # 配置隐形等待时间
    timeOut = GetConfig().timeOut
    # 配置输出日志级别
    loggingLevel = GetConfig().loggingLevel
    # 日志记录格式
    loggingFormat = "[时间:%(asctime)s] - [等级:%(levelname)s] - [文件名:%(filename)s] - [函数:%(funcName)s] - [行号:%(lineno)s] - [%(message)s]"
    # 案例运行浏览器类型
    browserType = GetConfig().browserType # Firefox\Chrome


    # Email
    # SMTP服务器登录名
    SMTPLoginName = GetConfig().SMTPLoginName
    # SMTP服务器登录密码
    SMTPLoginPasswd = GetConfig().SMTPLoginPasswd
    # 发件地址
    senderAddress = GetConfig().senderAddress
    # 收件地址
    receiversAddress = GetConfig().receiversAddress
    # SMTP服务器地址
    SMTPServerAddress = GetConfig().SMTPServerAddress
    # SMTP服务器地址端口
    SMTPPort = GetConfig().SMTPPort
    # 邮件主题
    emailTitle = GetConfig().emailTitle
    # 正文内容
    emailText = GetConfig().emailText
